package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    //手机号是否唯一
    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoMapper.selectContentByPhone(phone);
        return userInfo==null;
    }

    //发送短信
    @Override
    public void sendVerifyCode(String phone) throws LogicException {

        //验证码发送次数判断
        if(userInfoRedisService.PhoneCodeCount(phone) > 3L){
            throw new LogicException("发送频发请稍后再试");
        }

        //创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //创建一个短信，并发短信
        StringBuilder sb = new StringBuilder(100);
        sb.append("您注册的短信验证码是：").append(code).append("，请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");

        //将验证码缓存到redis中
        userInfoRedisService.setVerifyCode(phone,code);

    }

    //注册
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException{
        //1.是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //2.判断参数是否一致
        AssertUtil.isEquals(password,rpassword,"两次密码不一致");
        //3.判断手机号码格式是否正确


        //4.判断手机号码是否唯一
        if(!this.checkPhone(phone)){
            throw new LogicException("手机号已经被注册");
        }
        //5.判断验证码是否一致
        String verifyCode1 = userInfoRedisService.getVerifyCode(phone);
        if(!verifyCode.equals(verifyCode1)){
            throw new LogicException("验证码错误或已过期");
        }
        //6.用户注册
        UserInfo userInfo = new UserInfo();
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setState(UserInfo.STATE_NORMAL);
        userInfo.setPhone(phone);
        userInfoMapper.insert(userInfo);
    }


    /**
     * 用户登录
     * @param username
     * @param password
     */
    @Override
    public String selectUserByUsernameAndPassword(String username, String password) throws LogicException {
        UserInfo userInfo = userInfoMapper.selectUserByUsernameAndPassword(username,password);
        //如果查出来的结果为空则不存在 账号密码错误
        if(userInfo==null){
            throw new LogicException("账号或密码错误");
        }
        //生成随机key
        String token = UUID.randomUUID().toString().replace("-", "");
        //调用redis的服务方法将token和userInfo传进去 在redis中缓存
        userInfoRedisService.setUserInfo(token,userInfo);
       return token;

    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
